package com.ziq.zcamp.view.layoutmanager

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter

class CalendarGridLayoutManager(context: Context?, spanCount: Int, val mAdapter:GroupedRecyclerViewAdapter?, orientation:Int = RecyclerView.VERTICAL, reverseLayout:Boolean = false) : GridLayoutManager(context, spanCount,
    orientation,
    reverseLayout) {

    interface ChildSpanSize {
        fun getChildSpanSize(groupPosition: Int, childPosition: Int): Int
    }

    init {
        setSpanSizeLookup()
    }


    private fun setSpanSizeLookup() {
        super.setSpanSizeLookup(object : SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                if (mAdapter != null) {
                    val type: Int = mAdapter.judgeType(position)
                    if (type == GroupedRecyclerViewAdapter.TYPE_HEADER || type == GroupedRecyclerViewAdapter.TYPE_FOOTER) {
                        return spanCount
                    }
                }
                return 1
            }
        })
    }

    override fun smoothScrollToPosition(
        recyclerView: RecyclerView?,
        state: RecyclerView.State?,
        position: Int
    ) {
        val scroller = object : LinearSmoothScroller(recyclerView!!.context) {
            override fun getVerticalSnapPreference(): Int {
                return SNAP_TO_START
            }
        }
        scroller.targetPosition = position
        startSmoothScroll(scroller)
    }

}